package fm.douban;

import net.rim.device.api.i18n.ResourceBundle;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.PasswordEditField;
import net.rim.device.api.ui.component.TextField;
import net.rim.device.api.ui.container.FlowFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;

public class LoginDialog extends MainScreen {

	public TextField textUser = new TextField(Field.HIGHLIGHT_FOCUS
	/* | Field.AXIS_HORIZONTAL */| Field.USE_ALL_WIDTH);
	public PasswordEditField textPass = new PasswordEditField("", "", 20,
			Field.HIGHLIGHT_FOCUS /* | Field.AXIS_HORIZONTAL */
					| Field.USE_ALL_WIDTH);
	public FlowFieldManager ffm1 = new FlowFieldManager();
	public FlowFieldManager ffm2 = new FlowFieldManager();
	// public FlowFieldManager ffm3 = new FlowFieldManager();
	public VerticalFieldManager vm = new VerticalFieldManager();
	public LabelField username = new LabelField(
			SongPlayScreen._resources.getString(RadioResource.EMAIL));
	public LabelField pass = new LabelField(
			SongPlayScreen._resources.getString(RadioResource.CREATIENTIAL));
	public ButtonField loginBtn = new ButtonField("Login", Field.FIELD_HCENTER
			| ButtonField.CONSUME_CLICK);

	// public ButtonField exitBtn = new ButtonField("Exit");

	public LoginDialog() {
		prepScreen();
	}

	FieldChangeListener loginListener = new FieldChangeListener() {
		public void fieldChanged(Field field, int context) {
			System.out.println("user:" + textUser.getText());
			System.out.println("passwd:" + textPass.getText());
			if (!LoginUtil.login(textUser.getText(), textPass.getText())) {
				Dialog.alert(SongPlayScreen._resources
						.getString(RadioResource.LOGIN_ERROR));
			}
			LoginUtil.updateDB(textUser.getText(), textPass.getText());
			Radio theApp = (Radio) getApplication();
			theApp.pushScreen(new SongPlayScreen());
			// theApp.pushScreen(new ChannelScreen());
			getScreen().close();
		}
	};


	public static ResourceBundle _resources = ResourceBundle.getBundle(
			RadioResource.BUNDLE_ID, RadioResource.BUNDLE_NAME);
	private LabelField titleLabel = new LabelField(
			_resources.getString(RadioResource.APP_DESC));
	private BitmapField logo = new BitmapField(
			Bitmap.getBitmapResource("fm.png"), Field.FIELD_HCENTER);

	private void prepScreen() {
		titleLabel.setFont(titleLabel.getFont().derive(Font.PLAIN,
				titleLabel.getFont().getHeight() + 2));
		super.setTitle(titleLabel);

		loginBtn.setMargin(0, 70, 0, 0);
		loginBtn.setChangeListener(loginListener);
		XYEdges edges = new XYEdges(1, 1, 1, 1);
		Border border = BorderFactory.createSimpleBorder(edges);
		textUser.setBorder(border);
		textPass.setBorder(border);
		ffm1.add(username);
		ffm1.add(textUser);
		pass.setPadding(10, 0, 0, 0);

		ffm2.add(pass);
		ffm2.add(textPass);

		vm.add(logo);
		ffm1.setPadding(5, 0, 0, 0);
		vm.add(ffm1);
		vm.add(ffm2);
		vm.add(loginBtn);
		vm.setPadding(Display.getHeight() / 15, Display.getWidth() / 20, 0,
				Display.getWidth() / 20);
		add(vm);

	}

	protected boolean navigationClick(int status, int time) {
		return false;
	}
}
